// pages/details/details.js
const {
    getGoodsDetail
} = require("../../api/Home");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        collectionflag: false,
        imgs: [
            "/images/banner01.jpg",
            "/images/banner02.jpg",
            "/images/banner03.jpg",
            "/images/banner04.jpg",
        ],
        num: 0, //商品数量
        goods: {
            id: 2,
            goodsImage: "/images/banner01.jpg",
            goodsName: "商品名称",
            goodsImgs: [
                "/images/banner01.jpg",
                "/images/banner02.jpg",
                "/images/banner03.jpg",
                "/images/banner04.jpg",
            ],
            goodsAddress: "广州",
            goodsPrice: "200",
            goodsPriceOld: "300",
            goodsDetails: "/images/IMG_0466.jpg"
        }
    },
    collection() {
        let cartLists = {
            id: this.data.goods.id,
            goodsImage: this.data.goods.goodsImage,
            goodsName: this.data.goods.goodsName,
            goodsPrice: this.data.goods.goodsPrice,
            num: 1
        };
        this.setData({
            collectionflag: !this.data.collectionflag
        })
        if (!this.data.collectionflag) {
            let collectionlist = wx.getStorageSync("collectionlist");
            let index = collectionlist.findIndex(item => item.id == this.data.goods.id);
            collectionlist.splice(index, 1)
            wx.setStorageSync("collectionlist", collectionlist);
        } else {
            let collectionlist = wx.getStorageSync("collectionlist");
            if (!collectionlist) {
                collectionlist = [];
                collectionlist.push(cartLists);
                wx.setStorageSync("collectionlist", collectionlist);
            } else {
                let index = collectionlist.findIndex(item => item.id == this.data.goods.id);
                if (index == -1) {
                    collectionlist.push(cartLists);
                    wx.setStorageSync("collectionlist", collectionlist);
                }
            }

        }
    },
    // 数据缓存
    // 数据缓存
    // wx.setStorageSync
    // wx.getStorageSync
    // wx.removeStorageSync
    // wx.clearStorageSync
    // wx.getStorageInfoSync
    // 1.添加数据与修改数据
    // wx.setStorageSync('goodsListsData', ["这是商品数据列表-988888"])
    // 2.获取数据
    // let data = wx.getStorageSync("goodsListsData");
    // console.log(data);
    // 3.删除数据
    // wx.removeStorageSync("goodsListsData");
    // 4.清空数据
    // wx.clearStorageSync();
    // 5.获取缓存信息
    // let infoData = wx.getStorageInfoSync();
    // console.log(infoData);

    // 同步数据缓存
    // wx.setStorage
    // wx.getStorage
    // wx.removeStorage
    // wx.clearStorage
    // wx.getStorageInfo
    // 加入购物车
    setCart() {
        // 购物车数据结构
        let cartLists = {
            id: this.data.goods.id,
            goodsImage: this.data.goods.goodsImage,
            goodsName: this.data.goods.goodsName,
            goodsPrice: this.data.goods.goodsPrice,
            num: 1
        };
        // 数据添加数据缓存中
        // 1.判断数据缓存中是否存在
        let goodsCarList = wx.getStorageSync("goodsCarList"); //获取数据
        if (goodsCarList) {
            // 2.有数据.
            let index = goodsCarList.findIndex(item => item.id == this.data.goods.id);
            if (index != -1) { //有值返回下标，没有值返回-1
                // 2.1 判断是否有相同数据，数量加一
                goodsCarList[index].num += 1;
            } else {
                // 2.2 没有相同数据，添加数据
                goodsCarList.push(cartLists);
            }
            // 更新数据
            wx.setStorageSync('goodsCarList', goodsCarList)
        } else {
            // 3.没有数据，直接添加数据
            wx.setStorageSync('goodsCarList', [cartLists]);
        }
        // 更新商品数量
        this.setData({
            num: wx.getStorageSync('goodsCarList').length
        })

    },


    // 跳转购物车页面
    goCar() {
        wx.switchTab({
            url: '/pages/car/car',
        })
    },

    // 跳转首页
    goHome() {
        wx.switchTab({
            url: '/pages/home/home',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    // goPay(){
    //     console.log(this.data.goods.goodsName);
    //     let name = this.data.goods.goodsName
    //     wx.navigateTo({
    //         url:"/pages/pay/pay?name='88'"+name
    //     })
    // },
    onLoad: function (options) {

        console.log(options);
        getGoodsDetail({
            goods_id: options.id
        }).then((data) => {
            console.log(data);
            let {
                goods_id,
                goods_small_logo,
                goods_name,
                pics,
                goods_price,
                goods_introduce
            } = data.message;
            let goods = {
                id: goods_id,
                goodsImage: goods_small_logo,
                goodsName: goods_name,
                goodsImgs: pics,
                goodsAddress: "广州",
                goodsPrice: goods_price,
                goodsPriceOld: goods_price + 100,
                goodsDetails: goods_introduce
            }
            this.setData({
                goods
            })
            let collectionlist = wx.getStorageSync('collectionlist')
            if (collectionlist) {
                if (collectionlist.findIndex(item => item.id == this.data.goods.id) != -1) {
                    this.setData({
                        collectionflag: true
                    })
                }
            }
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            num: wx.getStorageSync('goodsCarList').length
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})